# Maintainer: Díaz Urbaneja Diego <sodomon2@gmail.com>
# Contributor: Díaz Urbaneja Diego <sodomon2@gmail.com>
pkgname=lua-pgsql
pkgver=1.6.7
pkgrel=0
pkgdesc="Lua binding for PostgreSQL"
url="https://github.com/arcapos/luapgsql"
arch="all"
license="BSD-3-Clause-Clear"
makedepends="libpq-dev"
options="!check" # make check not implemented
source="lua-pgsql-$pkgver.tar.gz::https://github.com/arcapos/luapgsql/archive/pgsql-$pkgver.tar.gz"
builddir="$srcdir/luapgsql-pgsql-1.6.7"

_luaversions="5.1 5.2 5.3 5.4"
for _v in $_luaversions; do
	makedepends="$makedepends lua$_v-dev"
	subpackages="$subpackages lua$_v-${pkgname#lua-}:_package"
done

prepare() {
	default_prepare

	local lver; for lver in $_luaversions; do
		cp -r "$builddir" "$builddir-$lver"
	done
}

build() {
	local lver; for lver in $_luaversions; do
	msg "Building for Lua $lver..."
	cd "$builddir-$lver"
	if [ "$lver" = "5.1" ]; then
		make CFLAGS=" -O3 -Wall -fPIC -I/usr/include -I/usr/include/lua$lver -I/usr/include/postgresql $CFLAGS" \
		LDADD="-L/usr/lib -lpq -llua" \
		LIBDIR="/usr/lib" \
		LUADIR="/usr/lib/lua/5.1"
	else
		make CFLAGS=" -O3 -Wall -fPIC -I/usr/include -I/usr/include/lua$lver -I/usr/include/postgresql $CFLAGS" \
		LDADD="-L/usr/lib -lpq /usr/lib/liblua-$lver.so.0" \
		LIBDIR="/usr/lib" \
		LUADIR="/usr/lib/lua/$lver"
	fi
	done
}

package() {
	mkdir -p "$pkgdir"
}

_package() {
	local lver="${subpkgname:3:3}"
	pkgdesc="$pkgdesc (for Lua $lver)"
	depends="lua$lver"
	install_if="$pkgname=$pkgver-r$pkgrel lua$lver"

	mkdir -p "$subpkgdir"
	cd "$builddir-$lver"
	mkdir -p "$subpkgdir"/usr/lib/lua/$lver
	cp -a pgsql.so "$subpkgdir"/usr/lib/lua/$lver
}

sha512sums="13c26b3bd5f86c80e4ba157e41968bc066b9d61f20ab856e78fc85d60b40151b1dfdf0ac95f9c972e57bfaed4bec9969780e607aed36d3d587364d1e387216d7  lua-pgsql-1.6.7.tar.gz"
